package com.member.api.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@SuppressWarnings("all")
public class TestYFHInterfaceByRefundPay {
	
	//接入方
	public static final String UNIONPAY_KEY_ACCESSID = "xxxxxxx"; // 接入方 银联平台分配的用户信息加密 key
	public static final String UNIONPAY_ORGCODE_ACCESSID = "xxxxxxx"; // 接入方 银联平台分配的OrgCode

	public static void main(String[] args) throws Exception {
      //退费
		refundPay();
	}

	//退费接口
	private static void refundPay() {
		Map<String, String> json = new HashMap<>();
		json.put("channelId", "xxxxxxx");// 渠道id，固定值，银联提供
		json.put("hospitalId", "xxxxxxx"); // 医院id ，银联提供
		json.put("timeStamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
		json.put("backUrl", "xxxxxxx");
		json.put("orderId", "YHFJ350000014332126002"); // 订单号，不能和消费支付的订单号相同，会报错
		json.put("origQryId", "17210817588576054582"); // queryId 支付成功后返回的queryId，
		json.put("txnAmt", "1");
		json.put("operatorNo", "");
		json.put("termId", "");
		json.put("reqReserved", "");
		System.out.println("接口入参：{}" + JSON.toJSONString(json));
		String url = "http://58.33.119.64/services/rest/v1/trade/RefundPay";// 测试环境地址
		String res = sendPostToCloudPlatform(url,
				JSON.toJSONString(json), UNIONPAY_KEY_ACCESSID, UNIONPAY_ORGCODE_ACCESSID);
		System.out.println("接口出參：{}" + JSON.toJSONString(res));
	}

	private static String sendPostToCloudPlatform(String urlTail, String json, String uninopayKey,
			String unionpayOrgCode) {
		String content = "";
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String authorization = generateSignature(json, uninopayKey);// privateKey 银联提供 key 用于用户信息加密
		HttpPost httpPost = new HttpPost(urlTail);
		// 请求头的设置
		httpPost.setHeader("OrgCode", unionpayOrgCode);// OrgCode
		httpPost.setHeader("Authorization", authorization);
		httpPost.setHeader("Accept", "application/json, text/plain, */*");
		httpPost.setHeader("Content-type", "application/json;charset=utf-8");
		// 参数配置 编码格式配置
		StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
		httpPost.setEntity(stringEntity);
		// 执行请求操作，并拿到结果（同步阻塞）
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
			content = EntityUtils.toString(response.getEntity());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content;
	}

	public static String generateSignature(String json, String authorizationKey) {
		String sign = null;
		try {
			sign = encode(JSONObject.parseObject(json), authorizationKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sign;
	}

	private static String PARAM_AND_CHAR = "&";
	private static String PARAM_ASSIGN_CHAR = "=";


	public static String encode(Map<String, Object> requestData, String authorizationKey)
			throws NoSuchAlgorithmException {
		String sortRespJson = buildSortJson(requestData) + authorizationKey;
		return md5(sortRespJson);
	}

	private static String buildSortJson(Map<String, Object> requestData) {
		StringBuffer sortSb = new StringBuffer();
		List<String> jsonKeys = new ArrayList<>();
		for (String key : requestData.keySet()) {
			jsonKeys.add(key);
		}
		Collections.sort(jsonKeys);
		for (String jsonKey : jsonKeys) {
			if (requestData.get(jsonKey) != null) {
				if (sortSb.length() > 0) {
					sortSb.append(PARAM_AND_CHAR);
				}
				sortSb.append(jsonKey).append(PARAM_ASSIGN_CHAR).append(requestData.get(jsonKey));
			}
		}
		return sortSb.toString();
	}

	/** 获得MD5加密算法 **/
	private static String md5(String inStr) {
		String result = "";
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			// 这句是关键
			md5.update(inStr.getBytes("UTF-8"));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte b[] = md5.digest();
		int i;
		StringBuffer buf = new StringBuffer("");
		for (int offset = 0; offset < b.length; offset++) {
			i = b[offset];
			if (i < 0) {
				i += 256;
			}
			if (i < 16) {
				buf.append("0");
			}
			buf.append(Integer.toHexString(i));
		}
		result = buf.toString();

		return result;
	}

	private static String sendPostToCloudPlatform(String urlTail, String json) {
		String content = "";
		CloseableHttpClient httpClient = HttpClients.createDefault();

		HttpPost httpPost = new HttpPost(urlTail);
		// 请求头的设置
		httpPost.setHeader("Accept", "application/json, text/plain, */*");
		httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
		// 参数配置 编码格式配置
		StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
		httpPost.setEntity(stringEntity);
		// 执行请求操作，并拿到结果（同步阻塞）
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
			content = EntityUtils.toString(response.getEntity());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content;
	}
}
